package com.cskaoyan.pool;

import com.cskaoyan.utils.JDBCUtils2;

import java.sql.Connection;
import java.util.LinkedList;

/**
 * 创建日期: 2022/11/18 09:51
 *
 * @author ciggar
 */
public class MyConnectionPoolV1 {

    // 连接的容器（可选 ：ArrayList | LinkedList | 数组 | 队列 ）
    // 从头部存，从尾部取
    static LinkedList<Connection> pool;

    static {

        // 初始化数据库连接池
        pool = new LinkedList<Connection>();
        for (int i = 0; i < 8; i++) {
            Connection newConnection = JDBCUtils2.getNewConnection();
            pool.addFirst(newConnection);
        }


    }


    // 获取连接
    public static Connection getConnection(){
        Connection connection = pool.removeLast();
        return connection;
    }

    // 回收连接
    public static void returnConnection(Connection connection){

        pool.addFirst(connection);

    }
}
